package org.netty.framework.httpserver.http;

import static io.netty.util.internal.ObjectUtil.checkNotNull;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.IllegalReferenceCountException;

/**
 * 重写DefaultFullHttpResponse why? 因为netty提供的DefaultFullHttpResponse不能设置content
 * 
 * @author chenfanglin
 * @date 2018年9月20日下午5:17:01
 *
 */
public class DefaultFullHttpResponse extends DefaultHttpResponse implements FullHttpResponse {

	private ByteBuf content;
	private final HttpHeaders trailingHeaders;

	/**
	 * Used to cache the value of the hash code and avoid
	 * {@link IllegalReferenceCountException}.
	 */
	private int hash;

	public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status) {
		this(version, status, Unpooled.buffer(0));
	}

	public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, ByteBuf content) {
		this(version, status, content, true);
	}

	public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders) {
		this(version, status, Unpooled.buffer(0), validateHeaders, false);
	}

	public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders,
			boolean singleFieldHeaders) {
		this(version, status, Unpooled.buffer(0), validateHeaders, singleFieldHeaders);
	}

	public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, ByteBuf content,
			boolean validateHeaders) {
		this(version, status, content, validateHeaders, false);
	}

	public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, ByteBuf content,
			boolean validateHeaders, boolean singleFieldHeaders) {
		super(version, status, validateHeaders);
		if (content == null) {
			throw new NullPointerException("content");
		}
		this.content = content;
		trailingHeaders = new DefaultHttpHeaders(validateHeaders);
	}

	public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, ByteBuf content, HttpHeaders headers,
			HttpHeaders trailingHeaders) {
		super(version, status, headers);
		this.content = checkNotNull(content, "content");
		this.trailingHeaders = checkNotNull(trailingHeaders, "trailingHeaders");
	}

	@Override
	public HttpHeaders trailingHeaders() {
		return trailingHeaders;
	}

	@Override
	public ByteBuf content() {
		return content;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(ByteBuf content) {
		if (content == null) {
			content = Unpooled.EMPTY_BUFFER;
		}
		this.content = content;
	}

	@Override
	public int refCnt() {
		return content.refCnt();
	}

	@Override
	public FullHttpResponse retain() {
		content.retain();
		return this;
	}

	@Override
	public FullHttpResponse retain(int increment) {
		content.retain(increment);
		return this;
	}

	@Override
	public FullHttpResponse touch() {
		content.touch();
		return this;
	}

	@Override
	public FullHttpResponse touch(Object hint) {
		content.touch(hint);
		return this;
	}

	@Override
	public boolean release() {
		return content.release();
	}

	@Override
	public boolean release(int decrement) {
		return content.release(decrement);
	}

	@Override
	public FullHttpResponse setProtocolVersion(HttpVersion version) {
		super.setProtocolVersion(version);
		return this;
	}

	@Override
	public FullHttpResponse setStatus(HttpResponseStatus status) {
		super.setStatus(status);
		return this;
	}

	@Override
	public FullHttpResponse copy() {
		return replace(content().copy());
	}

	@Override
	public FullHttpResponse duplicate() {
		return replace(content().duplicate());
	}

	@Override
	public FullHttpResponse retainedDuplicate() {
		return replace(content().retainedDuplicate());
	}

	@Override
	public FullHttpResponse replace(ByteBuf content) {
		FullHttpResponse response = new DefaultFullHttpResponse(protocolVersion(), status(), content, headers().copy(),
				trailingHeaders().copy());
		response.setDecoderResult(decoderResult());
		return response;
	}

	@Override
	public int hashCode() {
		int hash = this.hash;
		if (hash == 0) {
			if (content().refCnt() != 0) {
				try {
					hash = 31 + content().hashCode();
				} catch (IllegalReferenceCountException ignored) {
					// Handle race condition between checking refCnt() == 0 and using the object.
					hash = 31;
				}
			} else {
				hash = 31;
			}
			hash = 31 * hash + trailingHeaders().hashCode();
			hash = 31 * hash + super.hashCode();
			this.hash = hash;
		}
		return hash;
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof DefaultFullHttpResponse)) {
			return false;
		}

		DefaultFullHttpResponse other = (DefaultFullHttpResponse) o;

		return super.equals(other) && content().equals(other.content())
				&& trailingHeaders().equals(other.trailingHeaders());
	}

}
